package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/kataras/golog"
	"sdn-executor/core/engine"
	model "sdn-executor/model/gnmi"
	"sdn-executor/service"
	"sdn-executor/utils/erros"
)

func GetGNMIResultByRequestID(c *gin.Context) {
	requestId := c.Param("requestId")
	results, err := service.GetGNMIResultByRequestID(requestId)
	if err != nil {
		SendResponse(c, err, nil)
		return
	}

	SendResponse(c, erros.OK, results)
}

// ExecuteGNMICapCmd
// @Title Get user list of a group.
// @Description Get users related to a specific group.
// @Param  groupID  path  int  true  "Id of a specific group." "120"
// @Route /api/group/{groupID}/users [post]
func ExecuteGNMICapCmd(c *gin.Context) {
	query := model.QueryCap{}
	if err := c.ShouldBindJSON(&query); err != nil {
		SendResponse(c, erros.New(erros.CodeInvalidQueryParam, err.Error()), nil)
		return
	}

	response, e := service.ExecuteGNMICmd(&query, &query.BaseQuery, engine.MsgTypeGNMICap)
	if e != nil {
		golog.Error("doHandleGnmiRequest failed")
		SendResponse(c, e, nil)
		return
	}
	SendResponse(c, erros.OK, response)
}

func ExecuteGNMIGetCmd(c *gin.Context) {
	query := model.QueryGet{}
	if err := c.ShouldBindJSON(&query); err != nil {
		SendResponse(c, erros.New(erros.CodeInvalidQueryParam, err.Error()), nil)
		return
	}

	response, e := service.ExecuteGNMICmd(&query, &query.BaseQuery, engine.MsgTypeGNMIGet)
	if e != nil {
		golog.Error("doHandleGnmiRequest failed")
		SendResponse(c, e, nil)
		return
	}
	SendResponse(c, erros.OK, response)
}

func ExecuteGNMIUpdateCmd(c *gin.Context) {
	query := model.QueryUpdate{}
	if err := c.ShouldBindJSON(&query); err != nil {
		SendResponse(c, erros.New(erros.CodeInvalidQueryParam, err.Error()), nil)
		return
	}

	response, e := service.ExecuteGNMICmd(&query, &query.BaseQuery, engine.MsgTypeGNMIUpdate)
	if e != nil {
		golog.Error("doHandleGnmiRequest failed")
		SendResponse(c, e, nil)
		return
	}
	SendResponse(c, erros.OK, response)
}

func ExecuteGNMIReplaceCmd(c *gin.Context) {
	query := model.QueryUpdate{}
	if err := c.ShouldBindJSON(&query); err != nil {
		SendResponse(c, erros.New(erros.CodeInvalidQueryParam, err.Error()), nil)
		return
	}

	response, e := service.ExecuteGNMICmd(&query, &query.BaseQuery, engine.MsgTypeGNMIReplace)
	if e != nil {
		golog.Error("doHandleGnmiRequest failed")
		SendResponse(c, e, nil)
		return
	}
	SendResponse(c, erros.OK, response)
}

func ExecuteGNMIDeleteCmd(c *gin.Context) {
	query := model.QueryDelete{}
	if err := c.ShouldBindJSON(&query); err != nil {
		SendResponse(c, erros.New(erros.CodeInvalidQueryParam, err.Error()), nil)
		return
	}

	response, e := service.ExecuteGNMICmd(&query, &query.BaseQuery, engine.MsgTypeGNMIDelete)
	if e != nil {
		golog.Error("doHandleGnmiRequest failed")
		SendResponse(c, e, nil)
		return
	}
	SendResponse(c, erros.OK, response)
}
